/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Data;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author NGOCLINH-PC
 */
public class Category {

    private int CategoryID;
    private String CategoryName;
    private int Cstatus;
    //private ArrayList<Category> listCate;

//    public ArrayList<Category> getListCate() {
//        return listCate;
//    }
//
//    public void setListCate(ArrayList<Category> listCate) {
//        this.listCate = listCate;
//    }
    //entity
    public Category() {
    }

    public Category(int CategoryID, String CategoryName, int Cstatus) {
        this.CategoryID = CategoryID;
        this.CategoryName = CategoryName;
        this.Cstatus = Cstatus;
    }

    public int getCategoryID() {
        return CategoryID;
    }

    public void setCategoryID(int CategoryID) {
        this.CategoryID = CategoryID;
    }

    public String getCategoryName() {
        return CategoryName;
    }

    public void setCategoryName(String CategoryName) {
        this.CategoryName = CategoryName;
    }

    public int getCstatus() {
        return Cstatus;
    }

    public void setCstatus(int Cstatus) {
        this.Cstatus = Cstatus;
    }

    //DA  viet
    public static void Cinsert(String CateName, int CateStatus) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("insert into Category values('" + CateName + "','" + CateStatus + "')");
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "Insert Successful!");
                //response.sendRedirect("");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void Cupdate(int CateID, String CateName, int CateStatus) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("update Category SET CategoryName=?, Status=? WHERE CategoryID=?");
            st.setString(1, CateName);
            st.setInt(2, CateStatus);
            st.setInt(3, CateID);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "Update Successful!");
                //response.sendRedirect("DangKi.jsp");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void Cdelete(int CateID) {
        try {
            // PreparedStatement st = Connect.Connect.getCon().prepareStatement("DELETE Category FROM Category inner join Event ON Category.CategoryID=Event.CategoryID WHERE Category.CategoryID='" + CateID + "'");
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("DELETE FROM Category  WHERE CategoryID='" + CateID + "'");
            ResultSet rs = st.executeQuery();
            if (rs.next()) {

                JOptionPane.showMessageDialog(null, "DELETE Successful!");
                //response.sendRedirect("DangKi.jsp");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);

        }
    }

    public ArrayList<Category> Cselect() {
        try {
            ArrayList<Category> listCate = new ArrayList<Category>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM Category");
            while (rs.next()) {
                Category cate = new Category(rs.getInt(1), rs.getString(2), rs.getInt(3));
                listCate.add(cate);// 
            }
            return listCate;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public ArrayList<Category> CselectbyQuery(String query) {
        try {
            ArrayList<Category> listCate = new ArrayList<Category>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM Category where 1=1 " + query);
            while (rs.next()) {
                Category cate = new Category(rs.getInt(1), rs.getString(2), rs.getInt(3));
                listCate.add(cate);// 
            }
            return listCate;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public ArrayList<Category> CselectById(int id) {
        try {
            ArrayList<Category> listCate = new ArrayList<Category>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM Category where CategoryID=" + id + "");
            while (rs.next()) {
                Category cate = new Category(rs.getInt(1), rs.getString(2), rs.getInt(3));
                listCate.add(cate);
            }
            return listCate;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
